<template>
  <a-modal
    :open="open"
    width="950px"
    :title="rowData?.id ? '修改卫生装备维护保养' : '新增卫生装备维护保养'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <div class="flex">
      <div class="w-1/2 form-style-flex">
        <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '110px' } }">
          <a-form-item label="维护保养日期" class="w-full" name="maintenanceTime" v-bind="validateInfos.maintenanceTime">
            <a-date-picker v-model:value="formState.maintenanceTime" format="YYYY-MM-DD" allow-clear value-format="YYYY-MM-DD" />
          </a-form-item>
          <a-form-item label="装备名称" name="equipmentName" class="w-full" v-bind="validateInfos.equipmentName">
            <SelectTable v-model:value="formState.equipmentName" type="配置列表" custom-input empty-close @select="onSelect" />
          </a-form-item>
          <a-form-item label="设备序列号" class="w-full">
            <a-input v-model:value="formState.equipmentNumber" disabled placeholder="请输入" />
          </a-form-item>
          <a-form-item label="品牌" class="w-full">
            <a-input v-model:value="formState.brand" disabled placeholder="请输入" />
          </a-form-item>
          <a-form-item label="规格" class="w-full">
            <a-input v-model:value="formState.specifications" disabled placeholder="请输入" />
          </a-form-item>
          <a-form-item label="型号" class="w-full">
            <a-input v-model:value="formState.model" disabled placeholder="请输入" />
          </a-form-item>
          <a-form-item label="单位" class="w-full">
            <a-input v-model:value="formState.unit" disabled placeholder="请输入" />
          </a-form-item>
          <a-form-item label="操作员" class="w-full" name="empName">
            <SelectTable
              v-model:value="formState.empName"
              type="员工"
              custom-input
              empty-close
              @select="
                (e) => {
                  formState.empName = e.empName
                  formState.empId = e.empId
                }
              "
            />
          </a-form-item>
        </a-form>
      </div>
      <div class="w-1/2 pl-3">
        <template v-if="fileList.length > 0">
          <div class="flex flex-wrap max-h-[295px] overflow-auto">
            <div v-for="(item, index) in fileList" :key="index" class="w-[190px] h-[140px] mr-5 mb-2">
              <img alt="example" style="width: 100%; height: 100%; transition: transform 0.5s" :src="item.url" />
            </div>
          </div>
        </template>
        <template v-else>
          <a-empty />
        </template>
      </div>
    </div>
    <a-form class="form-style-flex" :label-col="{ style: { width: '110px' } }">
      <a-form-item label="维护保养内容" class="w-full">
        <a-textarea v-model:value="formState.content" :rows="6" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import userStore from '@/store/user'

const { empId, empName } = userStore()

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const fileList = ref([])
const formState = ref({
  maintenanceTime: '',
  equipmentName: '',
  empName,
  empId
})
const rulesState = ref({
  maintenanceTime: [{ required: true, message: '请选择借用日期' }],
  equipmentName: [{ required: true, message: '请选择装备名称' }]
})

const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)
watch(
  () => props.open,
  (val) => {
    if (val && props.rowData?.id) {
      formState.value = { ...props.rowData }
      if (props.rowData?.equipmentImages) {
        fileList.value = props.rowData?.equipmentImages.map((item) => {
          return {
            ...item,
            uid: item.id,
            name: item.url,
            status: 'done',
            url: item.url
          }
        })
      } else {
        fileList.value = []
        formState.value = {
          maintenanceTime: '',
          equipmentName: '',
          empName,
          empId
        }
      }
    }
  }
)

const onOk = async () => {
  const { code, msg } = await post('/war/equipmentMaintenance/doSaveEquipmentMaintenance', formState.value)
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('success')
  onCancel()
}
const onSelect = (row) => {
  formState.value = {
    ...formState.value,
    equipmentName: row?.equipmentName,
    equipmentNumber: row?.equipmentNumber,
    equipmentCode: row?.equipmentCode,
    unit: row?.unit,
    brand: row?.brand,
    specifications: row?.specifications,
    model: row?.model,
    id: props.rowData?.id,
    configAddr: row?.configAddr
  }
  getImageList()
}
// 图片
const getImageList = async () => {
  const res = await post('war/equipmentDistribution/getEquipmentImageList', { distributionId: formState.value.equipmentCode })
  if (res.code !== 0) return message.error(res.msg)
  fileList.value = res.data.map((item) => {
    return {
      ...item,
      uid: item.id,
      name: item.url,
      status: 'done',
      url: item.url
    }
  })
}
const onCancel = () => {
  fileList.value = []
  emit('update:open', false)
  resetFields()
}
</script>
<style lang="less" scoped></style>
